package com.ad.platform.controller;

import com.ad.payload.dto.PfCategoryDto;
import com.ad.payload.dto.trade.CategorySearch;
import com.ad.payload.dto.trade.TradeSubAddDto;
import com.ad.payload.dto.trade.TradeSubDto;
import com.ad.payload.dto.trade.TradeSubEditDto;
import com.ad.payload.enums.ResultCode;
import com.ad.payload.model.IdDto;
import com.ad.payload.model.PageDto;
import com.ad.payload.model.PageView;
import com.ad.payload.model.ResultBean;
import com.ad.payload.vo.PfCategoryVo;
import com.ad.payload.vo.trade.CategoryLeftVo;
import com.ad.payload.vo.trade.CategoryRightFirstVo;
import com.ad.payload.vo.trade.ParentCategoryVo;
import com.ad.payload.vo.trade.TradeVo;
import com.ad.platform.service.IPfCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author rclia
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/pfCategory")
public class PfCategoryController {

    @Autowired
    private IPfCategoryService iPfCategoryService;

    /**
     * 新增/更新 类别信息
     * @param pfCategoryDto
     * @param bindingResult
     * @return
     */
    @PostMapping("/noAuth/saveOrUpdatePfCategory")
    public ResultBean<String> saveOrUpdatePfCategory(@RequestBody @Valid PfCategoryDto pfCategoryDto, BindingResult bindingResult){

        for(ObjectError error : bindingResult.getAllErrors()){
            return ResultBean.build(ResultCode.VALIDATE_FAILED.getCode(),error.getDefaultMessage());
        }

        return iPfCategoryService.saveOrUpdate(pfCategoryDto) ? ResultBean.success("成功") : ResultBean.success("失败");
    }

    /**
     * 删除类别信息
     * @param idDto
     * @param bindingResult
     * @return
     */
    @PostMapping("/noAuth/deletePfCategory")
    public ResultBean<String> deletePfCategory(@RequestBody @Valid IdDto idDto, BindingResult bindingResult){

        for(ObjectError error : bindingResult.getAllErrors()){
            return ResultBean.build(ResultCode.VALIDATE_FAILED.getCode(),error.getDefaultMessage());
        }

        return iPfCategoryService.removeById(idDto.getId()) ? ResultBean.success("成功")
                : ResultBean.success("失败");
    }

    /**
     * 后台web端类别分页,数据量少,不做条件查询
     * @param pageDto
     * @return
     */
    @PostMapping("/noAuth/listPfCategory")
    public PageView<PfCategoryVo> listPfCategory(@RequestBody PageDto pageDto){
        PageView<PfCategoryVo> pfCategoryVoPageView =
                iPfCategoryService.selectPage(pageDto.getPageNo(), pageDto.getPageSize());
        return pfCategoryVoPageView;
    }


    /**
     * 根据行业id查询子行业列表
     *
     * @param tradeSubDto
     * */
    @PostMapping("/noAuth/pageListSubTradeVo")
    public PageView<TradeVo> pageListSubTradeVo(@RequestBody @Valid TradeSubDto tradeSubDto){
        return iPfCategoryService.pageListSubTradeVo(tradeSubDto);
    }



    /**
     * 添加子行业
     *
     * @param tradeSubAddDto
     * */
    @PostMapping("/noAuth/addSubTrade")
    public Long addSubTrade(@RequestBody @Valid TradeSubAddDto tradeSubAddDto){
        return iPfCategoryService.addSubTrade(tradeSubAddDto);
    }


    /**
     * 编辑子行业
     *
     * @param tradeSubEditDto
     * */
    @PostMapping("/noAuth/editSubTrade")
    public Long editSubTrade(@RequestBody @Valid TradeSubEditDto tradeSubEditDto){
        return iPfCategoryService.editSubTrade(tradeSubEditDto);
    }



    /**
     * 删除子行业
     *
     * @param idDto
     * */
    @PostMapping("/noAuth/deleteSubTrade")
    public Long deleteSubTrade(@RequestBody @Valid IdDto idDto){
        return iPfCategoryService.deleteSubTrade(idDto);
    }



    /**
     * 查询行业左侧
     *
     * @param categorySearch
     * */
    @PostMapping("/noAuth/queryCategoryLeftVo")
    public List<CategoryLeftVo> queryCategoryLeftVo(CategorySearch categorySearch){
        return iPfCategoryService.queryCategoryLeftVo(categorySearch);
    }


    /**
     * 查询行业右侧
     *
     * @param categorySearch
     * */
    @PostMapping("/noAuth/queryCategoryRightVo")
    public List<List<CategoryRightFirstVo>> queryCategoryRightVo(CategorySearch categorySearch){
        return iPfCategoryService.queryCategoryRightVo(categorySearch);
    }


    /**
     * 查询子行业中所有一级行业
     *
     * @param idDto 存放当前行业的主键id
     * */
    @PostMapping("/noAuth/queryParentCategoryVo")
    public List<ParentCategoryVo> queryParentCategoryVo(@RequestBody @Valid IdDto idDto){
        return iPfCategoryService.queryParentCategoryVo(idDto);
    }

}
